﻿using System;
namespace HuaCheng.Model
{
    /// <summary>
    /// 下位机与上位机指令、状态下达与上传通讯协议
    /// </summary>    
    public class CommandProtocol
    {

        #region Enum Define
        /// <summary>
        /// 开关机状态机枚举
        /// </summary>
        public enum EOnOffState
        {
            /// <summary>
            /// 开机
            /// </summary>
            On,
            /// <summary>
            /// 关机
            /// </summary>
            Off,
            /// <summary>
            /// 无效态
            /// </summary>
            Invalid
        };
        /// <summary>
        /// 启动暂停状态枚举
        /// </summary>
        public enum EStartPauseState
        {
            /// <summary>
            /// 启动
            /// </summary>
            Start,
            /// <summary>
            /// 暂停
            /// </summary>
            Pause,
            /// <summary>
            /// 无效态
            /// </summary>
            Invalid
        };
        #endregion
        #region int转byte数组方法
        /// <summary>
        /// //int转byte数组
        /// </summary>
        /// <param name="data">输入数据</param>
        /// <param name="lenth">数据所占字节数只能为 2或者4</param>
        /// <returns>返回byte数组</returns>
        public static byte[] Int2Bytes(int data, int lenth)//int转byte数组
        {
            byte[] temp = new byte[lenth];
            if (lenth == 2)
            {
                temp[0] = (byte)(data & 0x00ff);
                temp[1] = (byte)((data & 0xff00) >> 8);
            }
            else if (lenth == 4)
            {
                temp[0] = (byte)(data & 0x000000ff);
                temp[1] = (byte)((data & 0x0000ff00) >> 8);
                temp[2] = (byte)((data & 0x00ff0000) >> 16);
                temp[3] = (byte)((data & 0xff000000) >> 24);
            }
            return temp;
        }
        #endregion
        #region 装置运行控制方法
        /// <summary>
        /// 装置运行控制
        /// </summary>
        /// <param name="chEnable">通道有效性</param>
        /// <param name="dir">充放电方向</param>
        /// <param name="modelControl">模式控制</param>
        /// <param name="waveControl">波形控制</param>
        /// <param name="commandUpdata">指令更新</param>
        /// <param name="clientLimit">客户限幅启用标志位</param>
        /// <param name="isNotBattery">是否为电池标志位</param>
        /// <returns>指令byte[]</returns>
        public static byte[] runControl(bool chEnable, bool dir, int modelControl, int waveControl, bool commandUpdata, bool clientLimit, bool isBattery, bool? resiveFactor)
        {
            byte ch_enable = 0x00;
            byte Dir = 0x00;
            byte model_control = 0x00;
            byte wave_control = 0x00;
            byte command_updata = 0x00;
            byte client_limit = 0x00;
            byte is_battery = 0x00;
            int resive_Factor = 0x00;

            byte[] temp = new byte[2];

            if (chEnable == true) ch_enable = 0x01;
            else if (chEnable == false) ch_enable = 0x00;

            if (dir == true) Dir = 0x01;
            else if (dir == false) Dir = 0x00;

            switch (modelControl)
            {
                case 0: model_control = 0x00; break;
                case 1: model_control = 0x01; break;
                case 2: model_control = 0x02; break;
                case 3: model_control = 0x03; break;
                case 4: model_control = 0x04; break;
                default: break;
            }
            switch (waveControl)
            {
                case 0: wave_control = 0x00; break;
                case 1: wave_control = 0x01; break;
                case 2: wave_control = 0x02; break;
                case 3: wave_control = 0x03; break;
                case 4: wave_control = 0x04; break;
                default: break;
            }
            if (commandUpdata == true) command_updata = 0x01;
            else if (commandUpdata == false) command_updata = 0x00;

            if (clientLimit == true) client_limit = 0x01;
            else if (clientLimit == false) client_limit = 0x00;

            if (isBattery == true) is_battery = 0x00;
            else if (isBattery == false) is_battery = 0x01;
            if (resiveFactor == null)
            {
                temp[0] = (byte)((ch_enable) | (Dir << 1) | (model_control << 2) | (wave_control << 5));
                temp[1] = (byte)((command_updata) | (client_limit << 1) | (is_battery << 2) | (0x00));
            }
            else
            {
                bool _resiveFactor = (bool)resiveFactor;
                if (_resiveFactor)
                {
                    resive_Factor = 0x01;
                }
                else
                {
                    resive_Factor = 0x00;
                }
                temp[0] = (byte)((ch_enable) | (Dir << 1) | (model_control << 2) | (wave_control << 5));
                temp[1] = (byte)((command_updata) | (client_limit << 1) | (is_battery << 2) | (resive_Factor << 3) | (0x00));
            }

            return temp;
        }
        #endregion
        #region 并联模式设置方法
        /// <summary>
        /// 并联模式设置
        /// </summary>
        /// <param name="paraMode">并联模式 0-3之间取值，超过取默认值0</param>
        /// <param name="paraNum">并联设备数</param>
        /// <param name="shareVolt">公共电压选择</param>
        /// <returns>指令byte[]</returns>
        public static byte[] paraModeSet(int paraMode, int paraNum, int shareVolt)
        {
            byte[] temp = new byte[2];
            byte para_mode = 0x00;
            byte para_num = 0x00;
            byte para_shareVolt = 0x00;
            if (paraMode >= 0 && paraMode <= 3)
            {
                switch (paraMode)
                {
                    case 0: para_mode = 0x00; break;
                    case 1: para_mode = 0x01; break;
                    case 2: para_mode = 0x02; break;
                    case 3: para_mode = 0x03; break;
                    default: break;
                }
            }
            if (paraNum >= 2 && paraNum <= 8)
            {
                switch (paraNum)
                {
                    case 2: para_num = 0x01; break;
                    case 3: para_num = 0x02; break;
                    case 4: para_num = 0x03; break;
                    case 5: para_num = 0x04; break;
                    case 6: para_num = 0x05; break;
                    case 7: para_num = 0x06; break;
                    case 8: para_num = 0x07; break;
                    default: break;
                }
            }

            if (shareVolt >= 0 && shareVolt <= 7)
            {
                switch (shareVolt)
                {
                    case 0: para_shareVolt = 0x00; break;
                    case 1: para_shareVolt = 0x01; break;
                    case 2: para_shareVolt = 0x02; break;
                    case 3: para_shareVolt = 0x03; break;
                    case 4: para_shareVolt = 0x04; break;
                    case 5: para_shareVolt = 0x05; break;
                    case 6: para_shareVolt = 0x06; break;
                    case 7: para_shareVolt = 0x07; break;
                    default: break;
                }
            }

            temp[0] = (byte)((para_mode) | (para_num << 3) | (para_shareVolt << 6));//低位
            temp[1] = (byte)(shareVolt >> 2);//获取最高位
            return temp;
        }
        #endregion
        #region 并联数目选择方法
        /// <summary>
        /// 并联数目选择
        /// </summary>
        /// <param name="ch1">通道1</param>
        /// <param name="ch2">通道2</param>
        /// <param name="ch3">通道3</param>
        /// <param name="ch4">通道4</param>
        /// <param name="ch5">通道5</param>
        /// <param name="ch6">通道6</param>
        /// <param name="ch7">通道7</param>
        /// <param name="ch8">通道8</param>
        /// <returns></returns>
        public static int paraNumSelect(bool[] ch)
        {
            int temp = 0;
            for (int i = 0; i < 8; i++)
            {
                if (ch[i] == true) temp++;
            }
            return temp;

        }
        #endregion
        #region 并联通道选择方法
        /// <summary>
        /// 并联通道选择
        /// </summary>
        /// <param name="ch1">通道1</param>
        /// <param name="ch2">通道2</param>
        /// <param name="ch3">通道3</param>
        /// <param name="ch4">通道4</param>
        /// <param name="ch5">通道5</param>
        /// <param name="ch6">通道6</param>
        /// <param name="ch7">通道7</param>
        /// <param name="ch8">通道8</param>
        /// <returns>指令byte[]</returns>
        public static byte paraChSelect(bool[] ch)
        {
            byte temp = 0x00;
            temp = (byte)(ch[0] == true ? temp | 0x01 : temp & 0xFE);
            temp = (byte)(ch[1] == true ? temp | 0x02 : temp & 0xFD);
            temp = (byte)(ch[2] == true ? temp | 0x04 : temp & 0xFB);
            temp = (byte)(ch[3] == true ? temp | 0x08 : temp & 0xF7);
            temp = (byte)(ch[4] == true ? temp | 0x10 : temp & 0xEF);
            temp = (byte)(ch[5] == true ? temp | 0x20 : temp & 0xDF);
            temp = (byte)(ch[6] == true ? temp | 0x40 : temp & 0xBF);
            temp = (byte)(ch[7] == true ? temp | 0x80 : temp & 0x7F);
            return temp;
        }

        #endregion
        #region 获取反码求和的校验值
        /// <summary>
        /// 获取反码求和的校验值
        /// </summary>
        public static byte GetChecksum(byte[] frame, int startIdx, int len)
        {
            if (startIdx < 0 || len < 0 || startIdx + len > frame.Length)
                throw new Exception("Error Index");
            byte sum = 0;
            // 求和
            for (int i = startIdx; i < startIdx + len; i++)
                sum += frame[i];
            sum = Convert.ToByte((~sum) & 0xff);
            sum++;
            return sum;
        }
        #endregion
        #region 帧校验
        /// <summary>
        /// 校验帧,检查有无错误
        /// </summary>
        public static bool Checksum(byte[] frame, int startIdx, int len)
        {
            byte sum = 0;
            // 求和
            for (int i = startIdx; i < startIdx + len; i++)
                sum += frame[i];
            return Convert.ToByte((sum) & 0xff) == 0;
        }
        #endregion
        #region 独立模式参数包
        /// <summary>
        /// 独立模式参数包
        /// </summary>
        public class IndependParameter
        {
            #region 字段
            //版本号
            public int version;
            //指令长度
            public int Length = 56;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //工步方案号
            public int schemeNum;
            //工步号   最大4095步
            public int stepNum;
            // AC/DC侧直流母线电压设定
            public int dcBusVoltSet;
            // 通道有效性
            public bool chEnable;
            // 充放电方向   0：为输出能量（对应负载充电）；1：为吸收能量（对应负载放电）；
            public bool dir;
            /// <summary>
            /// 模式控制  
            /// 000：为恒流模式CC；
            /// 001：为恒压模式CV；
            /// 010：为恒功率模式CP；
            /// 011:  为CC/CV模式；
            /// 100: 为恒阻模式CR；
            /// </summary>
            public int modelControl;
            /// <summary>
            /// 波形控制
            /// 000为阶跃；即立即输出至新的设定值；
            /// 001为斜坡，即按照设定速率到达设定目标值；
            /// 010为脉冲，即输出指定的值1，持续指定的时间数1，变化到指定的值2，再持续指定的时间数2，返回值1，如此循环；
            /// 011为阶跃工况，即按照参数n所给的幅值输出，经过t时间后变化到参数n+1所给的幅值，运行t+1时间，如此循环，直至收到的新的时间值为零。
            /// 100为斜坡工况：
            /// </summary>
            public int waveControl;
            //指令更新   
            //指令参数修改后该位翻转
            public bool commandUpdata;
            // 客户限幅启用
            public bool clientLimit;
            // 是否为电池,0：电池测试；
            public bool isBattery;
            // 校正因子更新位
            public bool isCorrect;
            // 电压下限
            public int voltLimitLower;
            // 电压上限
            public int voltLimitUpper;
            // 电流下限
            public int currentLimitLower;
            // 电流上限
            public int currentLimitUpper;
            // 温度上限
            public int temperLimitUpper;
            // 客户上限幅,对实际运行电流的限幅
            public int clientUpper;
            // 客户下限幅,对实际运行电流的限幅
            public int clientLower;
            //参数1
            public float param1;
            //参数2
            public int param2;
            //参数3
            public float param3;
            //参数4
            public int param4;
            // 辅助通道电压上限
            public int auxVoltUpper;
            // 辅助通道电压下限
            public int auxVoltLower;
            // 辅助通道温度上限
            public int auxTemperUpper;
            // 辅助通道温度下限
            public int auxTemperLower;

            #endregion

            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0002);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(42, 2)[0];
                tmpCommand[11] = Int2Bytes(42, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;

                tmpCommand[14] = (byte)schemeNum;
                tmpCommand[15] = 0x00;
                tmpCommand[16] = Int2Bytes(stepNum, 2)[0];
                tmpCommand[17] = Int2Bytes(stepNum, 2)[1];
                tmpCommand[18] = Int2Bytes(dcBusVoltSet, 2)[0];
                tmpCommand[19] = Int2Bytes(dcBusVoltSet, 2)[1];
                tmpCommand[20] = runControl(chEnable, dir, modelControl, waveControl, commandUpdata, clientLimit, isBattery, null)[0];
                tmpCommand[21] = runControl(chEnable, dir, modelControl, waveControl, commandUpdata, clientLimit, isBattery, null)[1];
                tmpCommand[22] = Int2Bytes(voltLimitLower, 2)[0];
                tmpCommand[23] = Int2Bytes(voltLimitLower, 2)[1];
                tmpCommand[24] = Int2Bytes(voltLimitUpper, 2)[0];
                tmpCommand[25] = Int2Bytes(voltLimitUpper, 2)[1];
                tmpCommand[26] = Int2Bytes(currentLimitLower, 2)[0];
                tmpCommand[27] = Int2Bytes(currentLimitLower, 2)[1];
                tmpCommand[28] = Int2Bytes(currentLimitUpper, 2)[0];
                tmpCommand[29] = Int2Bytes(currentLimitUpper, 2)[1];
                tmpCommand[30] = Int2Bytes(temperLimitUpper, 2)[0];
                tmpCommand[31] = Int2Bytes(temperLimitUpper, 2)[1];

                tmpCommand[32] = Int2Bytes(clientUpper, 2)[0];
                tmpCommand[33] = Int2Bytes(clientUpper, 2)[1];
                tmpCommand[34] = Int2Bytes(clientLower, 2)[0];
                tmpCommand[35] = Int2Bytes(clientLower, 2)[1];

                tmpCommand[36] = BitConverter.GetBytes(param1)[0];
                tmpCommand[37] = BitConverter.GetBytes(param1)[1];
                tmpCommand[38] = BitConverter.GetBytes(param1)[2];
                tmpCommand[39] = BitConverter.GetBytes(param1)[3];

                tmpCommand[40] = Int2Bytes(param2, 2)[0];
                tmpCommand[41] = Int2Bytes(param2, 2)[1];

                tmpCommand[42] = BitConverter.GetBytes(param3)[0];
                tmpCommand[43] = BitConverter.GetBytes(param3)[1];
                tmpCommand[44] = BitConverter.GetBytes(param3)[2];
                tmpCommand[45] = BitConverter.GetBytes(param3)[3];

                tmpCommand[46] = Int2Bytes(param4, 2)[0];
                tmpCommand[47] = Int2Bytes(param4, 2)[1];

                tmpCommand[48] = Int2Bytes(auxVoltUpper, 2)[0];
                tmpCommand[49] = Int2Bytes(auxVoltUpper, 2)[1];
                tmpCommand[50] = Int2Bytes(auxVoltLower, 2)[0];
                tmpCommand[51] = Int2Bytes(auxVoltLower, 2)[1];
                tmpCommand[52] = Int2Bytes(auxTemperUpper, 2)[0];
                tmpCommand[53] = Int2Bytes(auxTemperUpper, 2)[1];
                tmpCommand[54] = Int2Bytes(auxTemperLower, 2)[0];
                tmpCommand[55] = Int2Bytes(auxTemperLower, 2)[1];

                tmpCommand[12] = GetChecksum(tmpCommand, 14, 42);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);

                return tmpCommand;
            }
        }
        #endregion
        #region 独立模式动作数据包
        /// <summary>
        /// 独立模式动作数据包
        /// </summary>
        public class IndependAction
        {
            #region properity
            //版本号
            public int version;
            //指令长度
            public int Length = 26;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //工步方案号
            public int schemeNum;
            //工步号
            public int stepNum;
            // 开关机   0xff00：开机
            public EOnOffState onOff = EOnOffState.Invalid;
            // 启动暂停
            public EStartPauseState startPause = EStartPauseState.Invalid;
            // 控制复位
            public int controlReset;
            // 系统复位  0xff：启动复位；其它：禁止复位；
            public int systemReset;
            // 风机启停
            public int fan;
            #endregion

            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0004);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(12, 2)[0];
                tmpCommand[11] = Int2Bytes(12, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;

                tmpCommand[14] = (byte)schemeNum;
                tmpCommand[15] = 0x00;
                tmpCommand[16] = Int2Bytes(stepNum, 2)[0];
                tmpCommand[17] = Int2Bytes(stepNum, 2)[1];
                tmpCommand[18] = 0x00;

                switch (onOff)
                {
                    case EOnOffState.On:
                        {
                            tmpCommand[19] = 0xFF;
                        } break;
                    case EOnOffState.Off:
                        {
                            tmpCommand[19] = 0x00;
                        } break;
                    case EOnOffState.Invalid:
                        {
                            tmpCommand[19] = 0xBB;
                        } break;
                    default:
                        break;
                }

                tmpCommand[20] = 0x00;

                switch (startPause)
                {
                    case EStartPauseState.Start:
                        {
                            tmpCommand[21] = 0xFF;
                        } break;
                    case EStartPauseState.Pause:
                        {
                            tmpCommand[21] = 0x00;
                        } break;
                    case EStartPauseState.Invalid:
                        {
                            tmpCommand[21] = 0xBB;
                        } break;
                    default:
                        break;
                }

                if (controlReset != 0)
                {
                    tmpCommand[22] = 0xFF;
                }
                else if (controlReset == 0)
                {
                    tmpCommand[22] = 0x00;
                }

                if (systemReset != 0)
                {
                    tmpCommand[23] = 0xFF;
                }
                else if (systemReset == 0)
                {
                    tmpCommand[23] = 0x00;
                }

                if (fan != 0)
                {
                    tmpCommand[24] = 0xFF;
                }
                else if (fan == 0)
                {
                    tmpCommand[24] = 0x00;
                }
                tmpCommand[25] = 0x00;

                tmpCommand[12] = GetChecksum(tmpCommand, 14, 12);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);

                return tmpCommand;
            }
        }
        #endregion
        #region 独立模式工况数据包
        public class WorkConditionParameter//工况参数类
        {
            public float param1;
            public UInt16 param2;
            public float param3;
            public UInt16 param4;
        }
        /// <summary>
        /// 独立模式工况数据包
        /// </summary>
        public class IndependWorkCondition              //
        {
            //版本号
            public int version;
            //指令长度
            public int Length = 378;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //尾包标识
            public bool isLast;
            //尾包中数据个数
            public int lastNum;
            //工况数据
            public WorkConditionParameter[] ConditionParameter;
            public IndependWorkCondition()
            {
                ConditionParameter = new WorkConditionParameter[30];
                for (int i = 0; i < 30; i++)
                {
                    ConditionParameter[i] = new WorkConditionParameter();
                }
            }

            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0003);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(364, 2)[0];
                tmpCommand[11] = Int2Bytes(364, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;

                for (int i = 0; i < 30; i++)
                {
                    tmpCommand[14 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param1)[0];
                    tmpCommand[15 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param1)[1];
                    tmpCommand[16 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param1)[2];
                    tmpCommand[17 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param1)[3];

                    tmpCommand[18 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param2)[0];
                    tmpCommand[19 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param2)[1];

                    tmpCommand[20 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param3)[0];
                    tmpCommand[21 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param3)[1];
                    tmpCommand[22 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param3)[2];
                    tmpCommand[23 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param3)[3];

                    tmpCommand[24 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param4)[0];
                    tmpCommand[25 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param4)[1];
                }
                if (isLast)
                {
                    tmpCommand[374] = 0xF0;
                    tmpCommand[375] = 0XF0;
                    tmpCommand[376] = BitConverter.GetBytes(lastNum)[0];
                    tmpCommand[377] = BitConverter.GetBytes(lastNum)[1];
                }
                else
                {
                    tmpCommand[374] = 0x0F;
                    tmpCommand[375] = 0X0F;
                    tmpCommand[376] = 0x00;
                    tmpCommand[377] = 0x00;
                }

                tmpCommand[12] = GetChecksum(tmpCommand, 14, 364);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);

                return tmpCommand;
            }
        }
        #endregion
        #region 并联模式参数包
        /// <summary>
        /// 并联模式参数包
        /// </summary>
        public class ParalleParameter
        {
            #region MyRegion
            //版本号
            public int version;
            //指令长度
            public int Length = 60;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //工步方案号
            public int schemeNum;
            // 工步号
            public int stepNum;
            // 直流母线电压设定
            public int dcBusVoltSet;
            //并联通道
            public bool[] ch = new bool[8];
            // 工作模式
            public int paraMode;
            // 并联通道数目
            public int paraNum;
            // 并联主通道号
            public int shareVolt;
            // 通道有效性设定。0无效
            public bool chEnable;
            // 充放电方向 0 充电
            public bool dir;
            // 模式控制
            public int modelControl;
            // 波形控制
            public int waveControl;
            // 指令更新,指令参数修改后该位翻转
            public bool commandUpdata;
            // 客户限幅启用,0无效
            public bool clientLimit;
            // 是否为电池 0是电池
            public bool isBattery;
            // 校正因子更新位,0：校正因子禁止更新
            public bool ResiveFactor;
            // 电压下限
            public int voltLimitLower;
            // 电压上限
            public int voltLimitUpper;
            // 电流下限
            public int currentLimitLower;
            // 电流上限
            public int currentLimitUpper;
            // 温度上限
            public int temperLimitUpper;
            // 客户上限幅
            public int clientUpper;
            // 客户下限幅
            public int clientLower;
            // 参数1
            public float param1;
            //参数2
            public int param2;
            //参数3
            public float param3;
            //参数4
            public int param4;
            // 辅助通道电压上限
            public int auxVoltUpper;
            // 辅助通道电压下限
            public int auxVoltLower;
            // 辅助通道电压上限
            public int auxTemperUpper;
            // 辅助通道电压下限
            public int auxTemperLower;
            #endregion
            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0005);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(46, 2)[0];
                tmpCommand[11] = Int2Bytes(46, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;
                tmpCommand[14] = (byte)schemeNum;
                tmpCommand[15] = 0x00;
                tmpCommand[16] = Int2Bytes(stepNum, 2)[0];
                tmpCommand[17] = Int2Bytes(stepNum, 2)[1];

                tmpCommand[18] = Int2Bytes(dcBusVoltSet, 2)[0];
                tmpCommand[19] = Int2Bytes(dcBusVoltSet, 2)[1];

                paraNum = paraNumSelect(ch)-1;

                tmpCommand[20] = paraChSelect(ch);
                tmpCommand[21] = 0x00;

                tmpCommand[22] = paraModeSet(paraMode, paraNum, shareVolt)[0];
                tmpCommand[23] = paraModeSet(paraMode, paraNum, shareVolt)[1];



                tmpCommand[24] = runControl(chEnable, dir, modelControl, waveControl, commandUpdata, clientLimit, isBattery, ResiveFactor)[0];
                tmpCommand[25] = runControl(chEnable, dir, modelControl, waveControl, commandUpdata, clientLimit, isBattery, ResiveFactor)[1];


                tmpCommand[26] = Int2Bytes(voltLimitLower, 2)[0];
                tmpCommand[27] = Int2Bytes(voltLimitLower, 2)[1];

                tmpCommand[28] = Int2Bytes(voltLimitUpper, 2)[0];
                tmpCommand[29] = Int2Bytes(voltLimitUpper, 2)[1];

                tmpCommand[30] = Int2Bytes(currentLimitLower, 2)[0];
                tmpCommand[31] = Int2Bytes(currentLimitLower, 2)[1];
                tmpCommand[32] = Int2Bytes(currentLimitUpper, 2)[0];
                tmpCommand[33] = Int2Bytes(currentLimitUpper, 2)[1];
                tmpCommand[34] = Int2Bytes(temperLimitUpper, 2)[0];
                tmpCommand[35] = Int2Bytes(temperLimitUpper, 2)[1];

                tmpCommand[36] = Int2Bytes(clientUpper, 2)[0];
                tmpCommand[37] = Int2Bytes(clientUpper, 2)[1];
                tmpCommand[38] = Int2Bytes(clientLower, 2)[0];
                tmpCommand[39] = Int2Bytes(clientLower, 2)[1];

                tmpCommand[40] = BitConverter.GetBytes(param1)[0];
                tmpCommand[41] = BitConverter.GetBytes(param1)[1];
                tmpCommand[42] = BitConverter.GetBytes(param1)[2];
                tmpCommand[43] = BitConverter.GetBytes(param1)[3];

                tmpCommand[44] = Int2Bytes(param2, 2)[0];
                tmpCommand[45] = Int2Bytes(param2, 2)[1];

                tmpCommand[46] = BitConverter.GetBytes(param3)[0];
                tmpCommand[47] = BitConverter.GetBytes(param3)[1];
                tmpCommand[48] = BitConverter.GetBytes(param3)[2];
                tmpCommand[49] = BitConverter.GetBytes(param3)[3];

                tmpCommand[50] = Int2Bytes(param4, 4)[0];
                tmpCommand[51] = Int2Bytes(param4, 4)[1];


                tmpCommand[52] = Int2Bytes(auxVoltUpper, 2)[0];
                tmpCommand[53] = Int2Bytes(auxVoltUpper, 2)[1];
                tmpCommand[54] = Int2Bytes(auxVoltLower, 2)[0];
                tmpCommand[55] = Int2Bytes(auxVoltLower, 2)[1];
                tmpCommand[56] = Int2Bytes(auxTemperUpper, 2)[0];
                tmpCommand[57] = Int2Bytes(auxTemperUpper, 2)[1];
                tmpCommand[58] = Int2Bytes(auxTemperLower, 2)[0];
                tmpCommand[59] = Int2Bytes(auxTemperLower, 2)[1];

                tmpCommand[12] = GetChecksum(tmpCommand, 14, 46);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);

                return tmpCommand;
            }
        }
        #endregion
        #region 并联模式动作数据包
        /// <summary>
        /// 并联模式动作数据包
        /// </summary>
        public class ParalleAction              
        {
            #region MyRegion
            //版本号
            public int version;
            //指令长度
            public int Length = 28;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //工步方案号
            public int schemeNum;
            //工步号
            public int stepNum;
            // 并联通道选择
            public bool[] ch = new bool[8];
            // 开关机
            public EOnOffState onOff = EOnOffState.Invalid;
            // 启动暂停
            public EStartPauseState startPause = EStartPauseState.Invalid;
            // 控制复位
            public int controlReset;
            // 系统复位
            public int systemReset;
            // 风机启停
            public int fan;

            #endregion
            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0007);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(14, 2)[0];
                tmpCommand[11] = Int2Bytes(14, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;
                tmpCommand[14] = (byte)schemeNum;
                tmpCommand[15] = 0x00;
                tmpCommand[16] = Int2Bytes(stepNum, 2)[0];
                tmpCommand[17] = Int2Bytes(stepNum, 2)[1];

                tmpCommand[18] = paraChSelect(ch);
                tmpCommand[19] = 0x00;
                tmpCommand[20] = 0x00;
                switch (onOff)
                {
                    case EOnOffState.On:
                        {
                            tmpCommand[21] = 0xFF;
                        } break;
                    case EOnOffState.Off:
                        {
                            tmpCommand[21] = 0x00;
                        } break;
                    case EOnOffState.Invalid:
                        {
                            tmpCommand[21] = 0xBB;
                        } break;
                    default:
                        break;
                }

                tmpCommand[22] = 0x00;

                switch (startPause)
                {
                    case EStartPauseState.Start:
                        {
                            tmpCommand[23] = 0xFF;
                        } break;
                    case EStartPauseState.Pause:
                        {
                            tmpCommand[23] = 0x00;
                        } break;
                    case EStartPauseState.Invalid:
                        {
                            tmpCommand[23] = 0xBB;
                        } break;
                    default:
                        break;
                }

                if (controlReset != 0)
                {
                    tmpCommand[24] = 0xFF;
                }
                else if (controlReset == 0)
                {
                    tmpCommand[24] = 0x00;
                }

                if (systemReset != 0)
                {
                    tmpCommand[25] = 0xFF;
                }
                else if (systemReset == 0)
                {
                    tmpCommand[25] = 0x00;
                }

                if (fan != 0)
                {
                    tmpCommand[26] = 0xFF;
                }
                else if (fan == 0)
                {
                    tmpCommand[26] = 0x00;
                }
                tmpCommand[27] = 0x00;

                tmpCommand[12] = GetChecksum(tmpCommand, 14, 14);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);
                return tmpCommand;
            }
        }
        #endregion
        #region 并联模式使能数据包
        /// <summary>
        /// 并联模式使能数据包
        /// </summary>
        public class ParallePulseEner              //并联模式使能数据包
        {
            //版本号
            public int version;
            //指令长度
            public int Length = 18;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //工步方案号
            public int schemeNum;
            //并联通道选择
            public bool[] ch = new bool[8];
            //脉冲使能
            public int pulseEn;

            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0008);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(4, 2)[0];
                tmpCommand[11] = Int2Bytes(4, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;

                tmpCommand[14] = paraChSelect(ch);
                tmpCommand[15] = 0x00;

                tmpCommand[16] = 0x00;
                if (pulseEn != 0)
                    tmpCommand[17] = 0xFF;
                else tmpCommand[17] = 0x00;

                tmpCommand[12] = GetChecksum(tmpCommand, 14, 4);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);
                
                return tmpCommand;
            }
        }
        #endregion
        #region 并联模式工况数据包
        public class ParalleWorkCondition              //并联模式工况数据包
        {
            //版本号
            public int version;
            //指令长度
            public int Length = 380;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //通道
            public bool[] ch = new bool[8];
            //尾包标识
            public bool isLast;
            //尾包中数据个数
            public int lastNum;
            //工况数据
            public WorkConditionParameter[] ConditionParameter;
            public ParalleWorkCondition()
            {
                ConditionParameter = new WorkConditionParameter[30];
                for (int i = 0; i < 30; i++)
                {
                    ConditionParameter[i] = new WorkConditionParameter();
                }
            }

            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0006);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(366, 2)[0];
                tmpCommand[11] = Int2Bytes(366, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;

                tmpCommand[14] = paraChSelect(ch);
                tmpCommand[15] = (byte)CID;

                for (int i = 0; i < 30; i++)
                {
                    tmpCommand[16 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param1)[0];
                    tmpCommand[17 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param1)[1];
                    tmpCommand[18 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param1)[2];
                    tmpCommand[19 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param1)[3];

                    tmpCommand[20 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param2)[0];
                    tmpCommand[21 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param2)[1];

                    tmpCommand[22 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param3)[0];
                    tmpCommand[23 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param3)[1];
                    tmpCommand[24 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param3)[2];
                    tmpCommand[25 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param3)[3];

                    tmpCommand[26 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param4)[0];
                    tmpCommand[27 + 12 * i] = BitConverter.GetBytes(ConditionParameter[i].param4)[1];
                }
                if (isLast)
                {
                    tmpCommand[376] = 0xF0;
                    tmpCommand[377] = 0XF0;
                    tmpCommand[378] = BitConverter.GetBytes(lastNum)[0];
                    tmpCommand[379] = BitConverter.GetBytes(lastNum)[1];
                }
                else
                {
                    tmpCommand[376] = 0x0F;
                    tmpCommand[377] = 0X0F;
                    tmpCommand[378] = 0x00;
                    tmpCommand[379] = 0x00;
                }
                tmpCommand[12] = GetChecksum(tmpCommand, 14, 366);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);
                return tmpCommand;
            }
        }
        #endregion
        #region 校正因子
        public class CaliFactor
        {
            public float range;
            public float k;
            public float b;
        }
        #region 电流校正因子数据包
        public class CurrentReviseFactor
        {
            //版本号
            public int version;
            //指令长度
            public int Length = 160;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //控制因子更新使能位,0XFF：使能；其它：禁止
            public int command_update_en;
            //保护用校正因子
            public CaliFactor[] displayCaliFactor;
            //控制用校正因子
            public CaliFactor[] controlCaliFactor;
            public CurrentReviseFactor()
            {
                displayCaliFactor = new CaliFactor[6];
                for (int i = 0; i < 6; i++)
                {
                    displayCaliFactor[i] = new CaliFactor();
                }
                controlCaliFactor = new CaliFactor[6];
                for (int i = 0; i < 6; i++)
                {
                    controlCaliFactor[i] = new CaliFactor();
                }
            }
            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0001);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(146, 2)[0];
                tmpCommand[11] = Int2Bytes(146, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;
                tmpCommand[14] = 0x00;
                tmpCommand[15] = (byte)command_update_en;

                for (int i = 0; i < 6; i++)
                {
                    tmpCommand[16 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].range)[0];
                    tmpCommand[17 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].range)[1];
                    tmpCommand[18 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].range)[2];
                    tmpCommand[19 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].range)[3];
                    tmpCommand[20 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].k)[0];
                    tmpCommand[21 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].k)[1];
                    tmpCommand[22 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].k)[2];
                    tmpCommand[23 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].k)[3];
                    tmpCommand[24 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].b)[0];
                    tmpCommand[25 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].b)[1];
                    tmpCommand[26 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].b)[2];
                    tmpCommand[27 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].b)[3];
                }
                for (int i = 0; i < 6; i++)
                {
                    tmpCommand[88 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].range)[0];
                    tmpCommand[89 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].range)[1];
                    tmpCommand[90 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].range)[2];
                    tmpCommand[91 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].range)[3];
                    tmpCommand[92 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].k)[0];
                    tmpCommand[93 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].k)[1];
                    tmpCommand[94 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].k)[2];
                    tmpCommand[95 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].k)[3];
                    tmpCommand[96 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].b)[0];
                    tmpCommand[97 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].b)[1];
                    tmpCommand[98 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].b)[2];
                    tmpCommand[99 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].b)[3];
                }
                tmpCommand[12] = GetChecksum(tmpCommand, 14, 146);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);
                return tmpCommand;
            }
        }
        #endregion

        #region 电压校正因子数据包
        public class VoltageReviseFactor
        {
            //版本号
            public int version;
            //指令长度
            public int Length = 160;
            //柜号
            public int GID;
            //中位机号
            public int Mid;
            //通道号
            public int CID;
            //控制因子更新使能位,0XFF：使能；其它：禁止
            public int command_update_en;
            //保护用校正因子
            public CaliFactor[] displayCaliFactor;
            //控制用校正因子
            public CaliFactor[] controlCaliFactor;
            public VoltageReviseFactor()
            {
                displayCaliFactor = new CaliFactor[6];
                for (int i = 0; i < 6; i++)
                {
                    displayCaliFactor[i] = new CaliFactor();
                }
                controlCaliFactor = new CaliFactor[6];
                for (int i = 0; i < 6; i++)
                {
                    controlCaliFactor[i] = new CaliFactor();
                }
            }
            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x0000);
                tmpCommand[4] = 0x00;
                tmpCommand[5] = 0x00;
                tmpCommand[6] = Int2Bytes(GID, 2)[0];
                tmpCommand[7] = Int2Bytes(GID, 2)[1];
                tmpCommand[8] = (byte)Mid;
                tmpCommand[9] = (byte)CID;
                tmpCommand[10] = Int2Bytes(146, 2)[0];
                tmpCommand[11] = Int2Bytes(146, 2)[1];
                tmpCommand[12] = 0x00;
                tmpCommand[13] = 0x00;
                tmpCommand[14] = 0x00;
                tmpCommand[15] = (byte)command_update_en;

                for (int i = 0; i < 6; i++)
                {
                    tmpCommand[16 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].range)[0];
                    tmpCommand[17 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].range)[1];
                    tmpCommand[18 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].range)[2];
                    tmpCommand[19 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].range)[3];
                    tmpCommand[20 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].k)[0];
                    tmpCommand[21 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].k)[1];
                    tmpCommand[22 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].k)[2];
                    tmpCommand[23 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].k)[3];
                    tmpCommand[24 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].b)[0];
                    tmpCommand[25 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].b)[1];
                    tmpCommand[26 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].b)[2];
                    tmpCommand[27 + 12 * i] = BitConverter.GetBytes(displayCaliFactor[i].b)[3];
                }
                for (int i = 0; i < 6; i++)
                {
                    tmpCommand[88 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].range)[0];
                    tmpCommand[89 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].range)[1];
                    tmpCommand[90 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].range)[2];
                    tmpCommand[91 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].range)[3];
                    tmpCommand[92 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].k)[0];
                    tmpCommand[93 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].k)[1];
                    tmpCommand[94 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].k)[2];
                    tmpCommand[95 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].k)[3];
                    tmpCommand[96 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].b)[0];
                    tmpCommand[97 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].b)[1];
                    tmpCommand[98 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].b)[2];
                    tmpCommand[99 + 12 * i] = BitConverter.GetBytes(controlCaliFactor[i].b)[3];
                }
                tmpCommand[12] = GetChecksum(tmpCommand, 14, 146);
                tmpCommand[13] = GetChecksum(tmpCommand, 0, 14);
                return tmpCommand;
            }
        }
        #endregion
        #endregion
        #region 广播包
        /// <summary>
        /// 广播包
        /// </summary>
        public class Broadcast              //广播包
        {
            //版本号
            public int version;
            //指令长度
            public int Length = 8;

            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x01);
                tmpCommand[4] = Int2Bytes(0, 2)[0];
                tmpCommand[5] = Int2Bytes(0, 2)[1];
                tmpCommand[6] = 0x00;
                tmpCommand[7] = GetChecksum(tmpCommand, 0, 8);

                return tmpCommand;
            }
        }
        #endregion
        #region 设备设置包
        /// <summary>
        /// 设备设置包
        /// </summary>
        public class DeviceConfig             //设备设置包
        {
            //版本号
            public int version;
            //指令长度
            public int Length = 13;
            //端口号
            public int Port;
            //柜号
            public int Gid;
            //中位机号
            public int Mid;

            public byte[] Command()
            {
                byte[] tmpCommand = new byte[Length];
                tmpCommand[0] = 0xAA;
                tmpCommand[1] = 0x55;
                tmpCommand[2] = (byte)version;
                tmpCommand[3] = (byte)(0x02);
                tmpCommand[4] = Int2Bytes(5, 2)[0];
                tmpCommand[5] = Int2Bytes(5, 2)[1];
                tmpCommand[6] = 0x00;
                tmpCommand[7] = 0x00;
                tmpCommand[8] = Int2Bytes(Port, 2)[0];
                tmpCommand[9] = Int2Bytes(Port, 2)[1];
                tmpCommand[10] = Int2Bytes(Gid, 2)[0];
                tmpCommand[11] = Int2Bytes(Gid, 2)[1];
                tmpCommand[12] = Int2Bytes(Mid, 2)[0];
                tmpCommand[6] = GetChecksum(tmpCommand, 8, 5);
                tmpCommand[7] = GetChecksum(tmpCommand, 0, 8);
                return tmpCommand;
            }
        }
        #endregion
    }
}
